'use strict';

const Service = require('egg').Service;

class WordService extends Service {
    //查询次文档是否存在
    async selectWord(title) {
        return await this.app.mysql.query('select * from wordlist where title=?',[title])
    }
    //创建
    async create(title,con,uid,s_id,time){
        await this.app.mysql.query('insert into wordlist (title,con,uid,s_id,create_time) values (?,?,?,?,?)',[title,con,uid,s_id,time])
    }
    //更新
    async update(title,con,id,time){
        await this.app.mysql.query('update wordlist set title=?,con=?,create_time=? where id=?',[title,con,time,id])
    }
    //查询
    async index(uid){
        let wordList = await this.app.mysql.query('select * from wordlist where uid=?',[uid])
        for(let i = 0;i<wordList.length;i++){
            let {uid,s_id} = wordList[i];
            let user = await this.app.mysql.query('select u.username from userlist u where id=?',[uid])
            let store = await this.app.mysql.query('select s.title from storelist s where id=?',[s_id])
            wordList[i].username = user[0].username;
            wordList[i].storeTitle = store[0].title;
        }
        return wordList
    }
    //删除
    async delete(id){
        await this.app.mysql.query('delete from wordlist where id=?',[id])
    }
    //查询指定知识库的所有文档
    async show(id){
        return await this.app.mysql.query('select * from wordlist where s_id=?',[id])
    }
}

module.exports = WordService;
